package org.wattdepot.cli.command;

import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Computes the carbon content of the source at at given timestamp.
 * 
 * @author Alex Kan and Kelvin Green
 * @version 2.0
 */
public class WattDepotCarbonContentCommand extends WattDepotCommand {

  /**
   * Computes the carbon content of the source at at given timestamp.
   * 
   * @param args contains an array of strings created from the user input.
   * @return a string of the carbon content requested.
   */
  public String doCommand(String[] args) {
    if (args.length != 5 || 0 != args[3].compareTo("sampling-interval")) {
      return "carboncontent {source} {timestamp} sampling-interval {interval}";
    }
    return getCarbonContent(args);
  }
  
  /**
   * Gets the carbon content of the source at at given timestamp.
   * 
   * @param args contains an array of strings created from the user input.
   * @return a string variable named output containing all the output for the method.
   */
  private String getCarbonContent(String[] args) {
    String output = "";
    boolean error = false;
    String source = args[1];
    String timestamp = args[2];
    Double carbonGenerated = null;
    Double energyGenerated = null;
    Double carbonContent = null;
    Integer samplingInterval = Integer.parseInt(args[4]);
    XMLGregorianCalendar startTS = null;
    XMLGregorianCalendar endTS = null;
    if (args.length != 5 || (0 != args[3].compareTo("sampling-interval"))) {
      return "Correct command is carboncontent {source} {timestamp} sampling-interval {interval}";
    }
    try {
      startTS = Tstamp.makeTimestamp(timestamp);
      endTS = Tstamp.incrementHours(startTS, 1);
      carbonGenerated = getClient().getCarbonEmitted(source, startTS, endTS, samplingInterval);
      energyGenerated = getClient().getEnergyGenerated(source, startTS, endTS, samplingInterval);
      energyGenerated = energyGenerated / 1000000;
      if (energyGenerated == 0) {
        output += "Carbon content not available.";
      }
      else {
        carbonContent = (carbonGenerated / energyGenerated);
        output += carbonContent + " lbs CO2/MWh";
      }
    }
    catch (MiscClientException mce) {
      error = true;
    }
    catch (BadXmlException bxe) {
      error = true;
    }
    catch (ResourceNotFoundException rnfe) {
      error = true;
    }
    catch (NotAuthorizedException nae) {
      error = true;
      if (error) {
        return "Unspecified error. Please contact support.";
      }
    }
    catch (NumberFormatException nfe) {
      error = true;
    }
    catch (Exception e) {
      return "Not a valid timestamp.";
    }
    return output;
  }

}
